//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.tuliu.appupdate.updatelib;

import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset;

public final class IOUtils {
    public static final Charset UTF8_CHARSET = Charset.forName("utf-8");
    public static final int DEFAULT_BUFFER_SIZE = 4096;

    private IOUtils() {
    }

    public static String toString(InputStream input) throws IOException {
        return toString((InputStream)input, 4096);
    }

    public static String toString(InputStream input, String charset) throws IOException {
        return toString(input, 4096, charset);
    }

    public static String toString(InputStream input, int bufferSize) throws IOException {
        return toString(input, bufferSize, (String)null);
    }

    public static String toString(InputStream input, int bufferSize, String charset) throws IOException {
        int avail = input.available();
        if(avail > bufferSize) {
            bufferSize = avail;
        }

        InputStreamReader reader = charset == null?new InputStreamReader(input, UTF8_CHARSET):new InputStreamReader(input, charset);
        return toString((Reader)reader, bufferSize);
    }

    public static String toString(Reader input) throws IOException {
        return toString((Reader)input, 4096);
    }

    public static String toString(Reader input, int bufSize) throws IOException {
        StringBuilder buf = new StringBuilder();
        char[] buffer = new char[bufSize];

        for(int n = input.read(buffer); -1 != n; n = input.read(buffer)) {
            if(n == 0) {
                throw new IOException("0 bytes read in violation of InputStream.read(byte[])");
            }

            buf.append(new String(buffer, 0, n));
        }

        input.close();
        return buf.toString();
    }

    public static void closeQuietly(Closeable in) {
        if(in != null) {
            try {
                in.close();
            } catch (IOException var2) {
                var2.printStackTrace();
            }
        }

    }
}
